package com.zuihai.app.mvp.ui.widget.comment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.dialog.OneButtonDialog;
import com.zuihai.app.mvp.ui.utils.DividerGridItemDecoration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 底部弹出评论框
 * Created by haibin
 * on 2016/11/10.
 * <p>
 * Changed by fei
 * on 2016/11/17
 *
 * @author Qiujuer
 */
@SuppressWarnings("unused")
public class BottomSheetPictureBar {

    private View mRootView;
    private EditText mEditText;
    private BottomSheetBehavior behavior;
    private Context mContext;
    private TextView mBtnCommit;
    private MyBottomDialog mDialog;
    private RecyclerView mRecyclerView;

    private boolean isFirstMax=true;

    // private EmojiView mEmojiView;
    private BottomSheetPictureBar(Context context) {
        this.mContext = context;
    }

    @SuppressLint("InflateParams")
    public static BottomSheetPictureBar delegation(Context context) {
        BottomSheetPictureBar bar = new BottomSheetPictureBar(context);
        bar.mRootView = LayoutInflater.from(context).inflate(R.layout.layout_bottom_sheet_comment_picture_bar, null, false);
        bar.initView();
        return bar;
    }

    private void initView() {
        mEditText = (EditText) mRootView.findViewById(R.id.et_comment);
        mBtnCommit = (TextView) mRootView.findViewById(R.id.btn_comment);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        initRecycler();
        KeyboardUtils.showSoftInput(mEditText);

        mBtnCommit.setEnabled(false);
        mDialog = new MyBottomDialog(mContext);
        mDialog.setContentView(mRootView);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                LogUtils.e("onDismiss");
                KeyboardUtils.hideSoftInput(mContext, mEditText);
                mDialog.dismiss();
//                mFrameLayout.setVisibility(View.GONE);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mBtnCommit.setEnabled(true);
                if (s.length()>1000){

                    if (isFirstMax){
                        OneButtonDialog errorDialog=new OneButtonDialog.Build(mContext)
                                .setConfirmListener(new OneButtonDialog.OnConfirmListener() {
                                    @Override
                                    public void onClick(OneButtonDialog dialog, View v) {
                                        dialog.dismiss();
                                    }
                                })
                                .setLogoResuorce(R.mipmap.zhanwei)
                                .setBottonText("知道了")
                                .setTextResource("评论最大字数限制1000字")
                                .create();
                        errorDialog.show();
                        isFirstMax=false;
                    }


                    mBtnCommit.setEnabled(false);
                }else {
                    mBtnCommit.setEnabled(true);
                    isFirstMax=true;
                }
            }
        });
    }

    private void initRecycler() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        mRecyclerView.setLayoutManager(layoutManager);
         DividerGridItemDecoration itemDecoration = new DividerGridItemDecoration(mContext, R.drawable.release_image_divider_shape);
          mRecyclerView.addItemDecoration(itemDecoration);

    }

    public void appendText(String text) {
        if (!TextUtils.isEmpty(text)) {
            mEditText.append(text);
        }
    }

    public void show(String hint) {
        mDialog.show();
        if (!"添加评论".equals(hint)) {
            mEditText.setHint(hint + " ");
        }

       /* mRootView.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                //KeyboardUtils.showSoftInput(mEditText);
            }
        }, 50);*/
    }

    public void dismiss() {
        KeyboardUtils.hideSoftInput(mContext, mEditText);
        mDialog.dismiss();
        LogUtils.e("dismiss");
    }

    public void setCommitListener(View.OnClickListener listener) {
        mBtnCommit.setOnClickListener(listener);
    }



    public void handleSelectFriendsResult(Intent data) {
        String names[] = data.getStringArrayExtra("names");
        if (names != null && names.length > 0) {
            String text = "";
            for (String n : names) {
                text += "@" + n + " ";
            }
            mEditText.getText().insert(mEditText.getSelectionEnd(), text);
        }
    }

    public EditText getEditText() {
        return mEditText;
    }

    public String getCommentText() {
        String result = "";
        if (mEditText.getText().toString().trim() != null) {
            Pattern p = Pattern.compile("(\r?\n(\\s*\r?\n)+)");
            Matcher m = p.matcher(mEditText.getText().toString().trim());
            result = m.replaceAll("\r\n\n");
        }
        return result;
    }

    public TextView getBtnCommit() {
        return mBtnCommit;
    }


//    public void hideSyncAction() {
//        mSyncToTweetView.setVisibility(View.INVISIBLE);
//        mSyncToTweetView.setText(null);
//    }

//    public void hideMentionAction() {
//        mAtView.setVisibility(View.INVISIBLE);
//    }

    /**
     * 默认显示的
     */
    public void showSyncAction() {
//        mSyncToTweetView.setVisibility(View.VISIBLE);
//        mSyncToTweetView.setText(R.string.send_tweet);
    }

//    public void showEmoji() {
//        mSyncToTweetView.setText(R.string.tweet_publish_title);
//        mFaceView.setVisibility(View.VISIBLE);
//        mFaceView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mEmojiView == null) {
//                    mEmojiView = new EmojiView(mContext, mEditText);
//                    mFrameLayout.addView(mEmojiView);
//                }
//                mFrameLayout.setVisibility(View.VISIBLE);
//                mEmojiView.openPanel();
//                TDevice.closeKeyboard(mEditText);
//            }
//        });
//
//        mEditText.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mFrameLayout.setVisibility(View.GONE);
//            }
//        });
//    }
}
